<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Загрузка приложения к лекции</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>


    <?
    session_start();

    $upload_dir = "attachments";
    $web_upload_dir = "attachments";

    if (isset($_COOKIE['msg']) && $_COOKIE['msg'] != '') {
        if (get_magic_quotes_gpc())
            $msg = stripslashes($_COOKIE['msg']);
        else
            $msg = $_COOKIE['msg'];
        setcookie('msg', '');
    }

    function saveHtml($s)
    {
        $s = str_replace("&", "&amp;", $s);
        $s = str_replace("<", "&lt;", $s);
        $s = str_replace(">", "&gt;", $s);
        $s = str_replace("'", "&apos;", $s);
        $s = str_replace("\"", "&quot;", $s);
        return $s;
    }

    ?>


    <link rel="stylesheet" type="text/css" href="styles.css"/>

    <script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
    <script type="text/javascript" src="Scripts/jquery.lightbox-0.5.js"></script>
    <script type="text/javascript" src="Scripts/MicrosoftAjax.js"></script>
    <script type="text/javascript">
        $(function () {
            $('a.logo').lightBox();
        });
    </script>
    <link rel="Stylesheet" type="text/css" href="Scripts/Content/jquery.lightbox-0.5.css" media="screen"/>

    <script>
        function stratchBorders() {
            var s = (document.getElementById('main_content').offsetHeight + 29).toString() + 'px';
            document.getElementById('left_border').style.height = s;
            document.getElementById('right_border').style.height = s;
        }
    </script>


</head>

<body onload="stratchBorders()" onresize="stratchBorders()">


<div id="wrapper">
<div class="header" style="background-image:url(images/top.jpg)">
    <div style="text-align:right; width:43%; float:left">
        <a href="index.html"><img src="images/logo.png"/></a>
    </div>
    <div style="text-align:left; margin:0 50%; width:35%;">
        <div style="width:100%; height:20px">&nbsp;</div>
        <div class="head_menu_item"><a href="subjects.php">Рубрики</a></div>
        <div class="head_menu_item"><a href="reading_places.php">Лектории&nbsp;</a></div>
        <div class="head_menu_item"><a href="courses.php">Предметы</a></div>
    </div>
</div>
<div class="container">
<div class="content">

<div class="search_bar">

    <div class="container">
        <div class="content">
            <div class="header" style="height:auto">
                <div class="container">
                    <div class="content">
                        <div class="decorate_line_search_bar"
                             style="background-image:url(images/search_bar_top.jpg)">&nbsp;</div>
                        <!--text-->
                    </div>
                    <!--content-->
                    <div class="decorate_corner_left_search_bar"
                         style="background-image:url(images/search_bar_corner_top_left.jpg)">&nbsp;</div>
                    <div class="decorate_corner_right_search_bar"
                         style="background-image:url(images/search_bar_corner_top_right.jpg)">&nbsp;</div>
                </div>
                <!--container-->
            </div>
            <!--header-->

            <div class="container">
                <div class="content">
                    <div class="text" style="height:25px; text-overflow: ellipsis; overflow: hidden;">
                        <div style="height:3px">&nbsp;</div>
                        <div style="float:left;">
                            <form action="">
                                <input type="text" size="40" id="name" class="input"/>
                                <input type="submit" value="Поиск" class="button"/>
                            </form>
                        </div>
                        <div style="text-align:left; margin:0 65%; width:35%;">
                            <div style="border-left:thick#000; width:auto; margin:0 0; text-align:right;">

                                <div class="head_menu_item">
                                    <a href="signin.php" style="font-size:16px">
                                    <? if (isset($_SESSION['uid'])) {
                                            echo $_SESSION['uid'];
                                        } else {
                                            echo "Вход";
                                        } ?>
                                    </a>
                                </div>
                                <div class="head_menu_item"><a href="registration.php" style="font-size:16px">Регистрация</a>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!--text-->
                </div>
                <!--content-->
                <div class="left" style="height:26px; background-image:url(images/search_bar_left.jpg)">
                    &nbsp;</div>
                <div class="right" style="height:26px; background-image:url(images/search_bar_right.jpg)">
                    &nbsp;</div>
                <div class="clear"></div>
            </div>

            <div class="footer" style="height:auto">
                <div class="container">
                    <div class="content">
                        <div class="decorate_line_search_bar"
                             style="background-image:url(images/search_bar_bottom.jpg)">&nbsp;</div>
                        <!--text-->
                    </div>
                    <!--content-->
                    <div class="decorate_corner_left_search_bar"
                         style="background-image:url(images/search_bar_corner_bottom_left.jpg)">&nbsp;</div>
                    <div class="decorate_corner_right_search_bar"
                         style="background-image:url(images/search_bar_corner_bottom_right.jpg)">&nbsp;</div
                        >
                </div>
                <!--container-->
            </div>
            <!--footer-->
        </div>
        <!--content-->
    </div>
    <!--container-->

</div>
<!--search_bar-->

<div class="text">
    <div class="header">
        <div class="container">
            <div class="content">
                <div class="decorate_line" style="background-image:url(images/content_top.jpg)">&nbsp;</div>
                <!--text-->
            </div>
            <!--content-->
            <div class="decorate_corner_left" style="background-image:url(images/corner_up_left.jpg)">
                &nbsp;</div>
            <div class="decorate_corner_right" style="background-image:url(images/corner_up_right.jpg)">
                &nbsp;</div>

        </div>
        <!--container-->
    </div>
    <!--header-->
    <div class="container">
        <div class="content">
            <div class="text" id="main_content">

                <? if (isset($_POST['description'])) { ?>
                <h1>Description saved</h1>
            <? } else { ?>
                <h1>Upload file:</h1>
                <? } ?>

                <div class="paragraph">
                <?php

                if (isset($_POST['description'])) {
                    $filename = $_POST['filename'];
                    $size = filesize($upload_dir . '/' . $filename);
                    $date = date('r', filemtime($upload_dir . '/' . $filename));
                    $description = saveHtml($_POST['description']);


                    $link = mysql_connect('localhost', 'root', 'the1soop');
                    if (!$link) {
                        die('Ошибка соединения: ' . mysql_error());
                    }
                    echo 'Успешно соединились';

                    $database = 'lectures';
                    if (!mysql_select_db($database)) {
                        echo "<h1>Can't select database</h1>";
                        // die("");
                    }

                    $url = $upload_dir . '/' . $filename;
                    $result = mysql_query("insert into attachment (attachment_type_id, name, description, url) values (1, '$filename', '$description', '$url')");
                    /*if (!$result) {
                        echo "<br/>Error during insert (attachment)";
                        echo mysqli_error($link);
                    }*/

                    mysqli_close($link);
                } elseif (isset($_POST['fileframe'])) {
                    $result = 'ERROR';
                    $result_msg = 'No FILE field found';
                    //$filename = '';

                    if (isset($_FILES['file'])) // file was send from browser
                    {
                        if ($_FILES['file']['error'] == UPLOAD_ERR_OK) // no error
                        {
                            $filename = $_FILES['file']['name']; // file name
                            move_uploaded_file($_FILES['file']['tmp_name'], $upload_dir . '/' . $filename);
                            $result = 'OK';
                        }
                        elseif ($_FILES['file']['error'] == UPLOAD_ERR_INI_SIZE)
                            $result_msg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
                        else
                            $result_msg = 'Unknown error';
                    }?>

<script language="JavaScript" type="text/javascript">
    var parDoc = window.parent.document;

                    <?  if ($result == 'OK') {  ?>
                parDoc.getElementById("upload_status").value = "file successfully uploaded";
                parDoc.getElementById("filename").value = <? echo $filename; ?>;
                parDoc.getElementById("filenamei").value = <? echo $filename; ?>;

                    <?
                }
                else {
                    ?>
                parDoc.getElementById("upload_status").value = <? echo "ERROR:" . $result_msg; ?>;
                    <? } ?>
            </script>

                <?
                } else {

                if (isset($msg)) {
                    ?>
                    <p style="font-weight: bold;"><? $msg ?></p>
                    <? } ?>


                <form action="<?=$PHP_SELF ?>" target="upload_iframe" method="post" enctype="multipart/form-data">
                    <input type="hidden" name="fileframe" value="true">
                    <label for="file">text file uploader:</label><br>
                    <input type="file" name="file" id="file" onChange="jsUpload(this)">
                </form>

                <script type="text/javascript">

                    function jsUpload(upload_field) {
                        var re_text = /\.txt|\.xml|\.zip|\.jpeg|\.jpg|\.pdf|\.ppt/i;
                        var filename = upload_field.value;

                        if (filename.search(re_text) == -1) {
                            alert("File does not have text(txt, xml, zip, jpeg, jpg, pdf, ppt) extension");
                            upload_field.form.reset();
                            return false;
                        }

                        upload_field.form.submit();
                        document.getElementById('upload_status').value = "uploading file...";
                        upload_field.disabled = true;
                        return true;
                    }
                </script>

                <iframe name="upload_iframe" style="width: 400px; height: 100px; display: none;">
                </iframe>

                <br>
                Upload status:<br>
                <input type="text" name="upload_status" id="upload_status"
                       value="not uploaded" size="64" disabled>
                <br><br>

                <form action="<?=$PHP_SELF?>" method="POST">
                    <input type="hidden" name="filename" id="filename">
                    <br><br>

                    <label for="photo">File description:</label><br>
                    <textarea rows="5" cols="50" name="description"></textarea>

                    <br><br>
                    <input type="submit" id="upload_button" value="save description">
                </form>
                <br>
                <? } ?>

                </div>
            </div>
            <!--text-->
        </div>
        <!--content-->
        <div class="left" id="left_border" style="background-image:url(images/content_left.jpg)">
            &nbsp;</div>
        <div class="right" id="right_border" style="background-image:url(images/content_right.jpg)">
            &nbsp;</div>
        <div class="clear"></div>
    </div>
    <!--container-->
    <div class="footer" style="height:auto">
        <div class="container">
            <div class="content">
                <div class="decorate_line" style="background-image:url(images/content_bottom.jpg)">
                    &nbsp;</div>
                <!--text-->
            </div>
            <!--content-->
            <div class="decorate_corner_left" style="background-image:url(images/corner_bottom_left.jpg)">
                &nbsp;</div>
            <div class="decorate_corner_right" style="background-image:url(images/corner_bottom_right.jpg)">
                &nbsp;</div>

        </div>
        <!--container-->
    </div>
    <!--footer-->
</div>
<!--text-->
</div>
<!--content-->
<div class="clear"></div>
</div>
<!--container-->
<div class="footer" style="background-image:url(images/foot.jpg)"></div>
</div>
<!--warapper-->
<div style="text-indent: -9999px;">
</div>


</body>

</html>
